/**
 * Copyright (c) 2011 Paul Wagner
 */
package com.pew.tasktimer.domain;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.log4j.Logger;

/**
 * @author Elwin
 * Created: Mar 25, 2011
 * Purpose: Houses the data structure of a task.
 *
 */
@XmlRootElement(name="task")
public class Task {

	static Logger cLogger = Logger.getLogger(Task.class);
	
	//Unique ID
	@XmlElement(required=false, defaultValue="-1")
	protected long clUniqueID = -1;
	
	//Basic Data
	@XmlElement
	protected long clStartTime = -1;
	@XmlElement
	protected long clStopTime = -1;
	@XmlElement
	protected long clTotalTime = -1;  //calculated value
	@XmlElement
	protected boolean cbIsRunning = false;
	@XmlElement
	protected String cStrNote = "";
	@XmlElement
	protected String cStrCsvTags = "";
	@XmlElement
	protected String cStrProject = "";
	
	//Derived Once
	//Start information
	@XmlElement
	protected int ciStartDayOfMonth = -1;
	@XmlElement
	protected int ciStartMonth = -1;
	@XmlElement
	protected int ciStartYear = -1;
	@XmlElement
	protected int ciStartHour = -1;
	@XmlElement
	protected int ciStartMin = -1;
	@XmlElement
	protected int ciStartSec = -1;
	
	//Stop information
	@XmlElement
	protected int ciStopDayOfMonth = -1;
	@XmlElement
	protected int ciStopMonth = -1;
	@XmlElement
	protected int ciStopYear = -1;
	@XmlElement
	protected int ciStopHour = -1;
	@XmlElement
	protected int ciStopMin = -1;
	@XmlElement
	protected int ciStopSec = -1;
	
	//Calculated
	//Difference information
	@XmlElement
	protected int ciDiffHour = -1;
	@XmlElement
	protected int ciDiffMin = -1;
	@XmlElement
	protected int ciDiffSec = -1;
	@XmlElement
	protected double cfDiffHourF = 0.0;
	
	//Other Data
	@XmlElement
	protected boolean isPotentialyBad = false;
	@XmlElement
	protected String cStrPotentialyBad = "";
	
	public Task() {
		setUniqueID();
	}
	
	public long getUniqueID() {
		return clUniqueID;
	}
	
	/**
	 * Sets or resets the unique ID.
	 * Created for legacy XML storage.
	 */
	public void setUniqueID() {
		clUniqueID = System.currentTimeMillis();
	}
	
	public void startTimer() {
		clStartTime = System.currentTimeMillis();
		fillInStartInfo();
		cbIsRunning = true;
	}
	
	public void stopTimer() {
		stopTimer(System.currentTimeMillis());
	}
	
	public void stopTimer(long myTime) {
		clStopTime = myTime;
		fillInStopInfo();
		fillInDiffInfo();
		cbIsRunning = false;
	}
	
	public boolean isRunning() {
		if (clStopTime <= 0)
		{
			cbIsRunning = true;
		}
		else {
			cbIsRunning = false;
		}
		
		return cbIsRunning;
	}
	
	protected void fillInStartInfo() {
		Date lDate = new Date(clStartTime);
		Calendar lCal = Calendar.getInstance();
		lCal.setTime(lDate);
		
		ciStartDayOfMonth = lCal.get(Calendar.DAY_OF_MONTH);
		ciStartMonth = lCal.get(Calendar.MONTH) + 1;
		ciStartYear = lCal.get(Calendar.YEAR);
		ciStartHour = lCal.get(Calendar.HOUR_OF_DAY);
		ciStartMin = lCal.get(Calendar.MINUTE);
		ciStartSec = lCal.get(Calendar.SECOND);
		
	}
	
	void fillInStartInfo_p() {
		fillInStartInfo();
	}
	
	protected void fillInStopInfo() {
		if (clStopTime <= clStartTime)
		{
			//nothing to do
			return;
		}
		
		Date lDate = new Date(clStopTime);
		Calendar lCal = Calendar.getInstance();
		lCal.setTime(lDate);
		
		ciStopDayOfMonth = lCal.get(Calendar.DAY_OF_MONTH);
		ciStopMonth = lCal.get(Calendar.MONTH) + 1;
		ciStopYear = lCal.get(Calendar.YEAR);
		ciStopHour = lCal.get(Calendar.HOUR_OF_DAY);
		ciStopMin = lCal.get(Calendar.MINUTE);
		ciStopSec = lCal.get(Calendar.SECOND);
	}
	
	void fillInStopInfo_p() {
		fillInStopInfo();
	}
	
	protected void fillInDiffInfo() {
		
		if(clStopTime <= clStartTime)
		{
			//nothing to do
			return;
		}
		
		clTotalTime = clStopTime - clStartTime;
		Long lLong = new Long(clTotalTime / 1000);  //change to seconds
		int lIntSecs = lLong.intValue();
		ciDiffHour = lIntSecs / 3600;
		ciDiffMin = (lIntSecs % 3600) / 60;
		ciDiffSec = lIntSecs % 216000;
		cfDiffHourF = lIntSecs / 3600.00;
	}
	
	
	
	/**
	 * @return the cStrNote
	 */
	public String getNote() {
		return cStrNote;
	}

	/**
	 * @param cStrNote the cStrNote to set
	 */
	public void setcStrNote(String cStrNote) {
		this.cStrNote = cStrNote;
	}

	/**
	 * @return the cStrCsvTags
	 */
	public String getCsvTags() {
		return cStrCsvTags;
	}

	/**
	 * @param cStrCsvTags the cStrCsvTags to set
	 */
	public void setcStrCsvTags(String cStrCsvTags) {
		this.cStrCsvTags = cStrCsvTags;
	}

	/**
	 * @return the cStrProject
	 */
	public String getProject() {
		return cStrProject;
	}

	/**
	 * @param cStrProject the cStrProject to set
	 */
	public void setcStrProject(String cStrProject) {
		this.cStrProject = cStrProject;
	}

	/**
	 * @return the clStartTime
	 */
	public long getClStartTime() {
		return clStartTime;
	}

	/**
	 * @return the clStopTime
	 */
	public long getClStopTime() {
		return clStopTime;
	}

	/**
	 * @return the clTotalTime
	 */
	public long getClTotalTime() {
		return clTotalTime;
	}

	/**
	 * @return the ciStartDayOfMonth
	 */
	public int getCiStartDayOfMonth() {
		return ciStartDayOfMonth;
	}

	/**
	 * @return the ciStartMonth
	 */
	public int getCiStartMonth() {
		return ciStartMonth;
	}

	/**
	 * @return the ciStartYear
	 */
	public int getCiStartYear() {
		return ciStartYear;
	}

	/**
	 * @return the ciStartHour
	 */
	public int getCiStartHour() {
		return ciStartHour;
	}

	/**
	 * @return the ciStartMin
	 */
	public int getCiStartMin() {
		return ciStartMin;
	}

	/**
	 * @return the ciStartSec
	 */
	public int getCiStartSec() {
		return ciStartSec;
	}

	/**
	 * @return the ciStopDayOfMonth
	 */
	public int getCiStopDayOfMonth() {
		return ciStopDayOfMonth;
	}

	/**
	 * @return the ciStopMonth
	 */
	public int getCiStopMonth() {
		return ciStopMonth;
	}

	/**
	 * @return the ciStopYear
	 */
	public int getCiStopYear() {
		return ciStopYear;
	}

	/**
	 * @return the ciStopHour
	 */
	public int getCiStopHour() {
		return ciStopHour;
	}

	/**
	 * @return the ciStopMin
	 */
	public int getCiStopMin() {
		return ciStopMin;
	}

	/**
	 * @return the ciStopSec
	 */
	public int getCiStopSec() {
		return ciStopSec;
	}

	/**
	 * @return the ciDiffHour
	 */
	public int getCiDiffHour() {
		return ciDiffHour;
	}

	/**
	 * @return the ciDiffMin
	 */
	public int getCiDiffMin() {
		return ciDiffMin;
	}

	/**
	 * @return the ciDiffSec
	 */
	public int getCiDiffSec() {
		return ciDiffSec;
	}

	/**
	 * @return the cfDiffHourF
	 */
	public double getCfDiffHourF() {
		return cfDiffHourF;
	}
	
	

	/**
	 * @return the isPotentialyBad
	 */
	public boolean isPotentialyBad() {
		return isPotentialyBad;
	}

	/**
	 * @param isPotentialyBad the isPotentialyBad to set
	 */
	public void setPotentialyBad(boolean isPotentialyBad) {
		this.isPotentialyBad = isPotentialyBad;
	}

	/**
	 * @return the cStrPotentialyBad
	 */
	public String getStrPotentialyBad() {
		return cStrPotentialyBad;
	}

	/**
	 * @param cStrPotentialyBad the cStrPotentialyBad to set
	 */
	public void setcStrPotentialyBad(String cStrPotentialyBad) {
		this.cStrPotentialyBad = cStrPotentialyBad;
	}

	
	
	public void setStartTime(long clStartTime) {
		this.clStartTime = clStartTime;
		fillInStartInfo();
	}
	/**
	 * @param clStopTime the clStopTime to set
	 */
	public void setStopTime(long clStopTime) {
		this.clStopTime = clStopTime;
		
		fillInStopInfo();
		fillInDiffInfo();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Task [cStrCsvTags=" + cStrCsvTags + ", cStrNote=" + cStrNote
				+ ", cStrProject=" + cStrProject + ", cbIsRunning="
				+ cbIsRunning + ", cfDiffHourF=" + cfDiffHourF
				+ ", ciDiffHour=" + ciDiffHour + ", ciDiffMin=" + ciDiffMin
				+ ", ciDiffSec=" + ciDiffSec + ", ciStartDayOfMonth="
				+ ciStartDayOfMonth + ", ciStartHour=" + ciStartHour
				+ ", ciStartMin=" + ciStartMin + ", ciStartMonth="
				+ ciStartMonth + ", ciStartSec=" + ciStartSec
				+ ", ciStartYear=" + ciStartYear + ", ciStopDayOfMonth="
				+ ciStopDayOfMonth + ", ciStopHour=" + ciStopHour
				+ ", ciStopMin=" + ciStopMin + ", ciStopMonth=" + ciStopMonth
				+ ", ciStopSec=" + ciStopSec + ", ciStopYear=" + ciStopYear
				+ ", clStartTime=" + clStartTime + ", clStopTime=" + clStopTime
				+ ", clTotalTime=" + clTotalTime + "]";
	}
}
